package com.lottery.ruok.lottrey.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.BindBankActivity;
import com.lottery.ruok.lottrey.activity.ChuantongcaiActivity;
import com.lottery.ruok.lottrey.activity.LiuhecaiDetailActivity;
import com.lottery.ruok.lottrey.activity.LoginActivity;
import com.lottery.ruok.lottrey.activity.MoneyPasswordActivity;
import com.lottery.ruok.lottrey.activity.OfficalLotteryDetailActivity;
import com.lottery.ruok.lottrey.activity.WithdrawCashActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetProfileRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetProfileResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import utils.Utils;

/**
 * Created by ASUS on 2017/7/29.
 */

public class BaseFragment extends Fragment{
//    protected Tracker mTracker;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        MyApplication application = (MyApplication) getActivity().getApplication();
//        mTracker = application.getDefaultTracker();
    }

//    public void sendAnalyticMsg(String msg){
//        mTracker.send(new HitBuilders.EventBuilder()
//                .setCategory(Common.LOTTERY_ANDROID_ACTION)
//                .setAction(msg)
//                .build());
//    }

    public void gotoLoginActivity(){
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
    }

    public void gotoLotteryDetailActivity(String code, int index){//0 其它 1官方2六合3传统
        switch (index) {
            case 1: {
                Intent intent = new Intent(getActivity(), OfficalLotteryDetailActivity.class);
                intent.putExtra("code", code);
                startActivity(intent);
                break;
            }
            case 2: {
                Intent intent = new Intent(getActivity(), LiuhecaiDetailActivity.class);
                intent.putExtra("code", code);
                startActivity(intent);
                break;
            }
            case 3: {
                Intent intent = new Intent(getActivity(), ChuantongcaiActivity.class);
                intent.putExtra("code", code);
                startActivity(intent);
                break;
            }
        }
    }

    protected void withdrawMoney() {
        updateAccount(new AfterUpdateAccount() {
            @Override
            public void afterUpdateAccount(final GetProfileResponse getProfileResponse) {
                if (getProfileResponse.getUser().getWithdraw_password() == 1) {
                    if (getProfileResponse.getUser().getWithdraw_account() == null || getProfileResponse.getUser().getWithdraw_account().equals("")) {
                        Utils.createDialog(getActivity(), "您没有绑定银行卡，请第一时间绑定银行卡", "去绑定", "知道了",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        Intent intent = new Intent(getActivity(), BindBankActivity.class);
                                        intent.putExtra("name", getProfileResponse.getUser().getWithdraw_realname());
                                        startActivity(intent);
                                    }
                                },
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                });
                    } else {
                        Intent intent = new Intent(getActivity(), WithdrawCashActivity.class);
                        startActivity(intent);
                    }
                } else {
                    Utils.createDialog(getActivity(), "您没有设置资金密码，请第一时间设置资金密码", "去设置", "知道了",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Intent intent = new Intent(getActivity(), MoneyPasswordActivity.class);
                                    startActivity(intent);
                                }
                            },
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                }
                            });
                }
            }
        }, true);
    }

    /**
     * 让子类实现
     *
     * @param data
     */
    public void setDatas(GetWholeCategoryResponse data) {

    }

    /**
     * 让子类实现
     */
    public void setSwipState() {

    }

    interface AfterUpdateAccount {
        void afterUpdateAccount(GetProfileResponse getProfileResponse);
    }

    protected void updateAccount(final AfterUpdateAccount afterUpdateAccount){
        updateAccount(afterUpdateAccount, false);
    }

    protected void updateAccount(final AfterUpdateAccount afterUpdateAccount, final boolean showProgres){
        RetrofitBase.getRetrofit().create(GetProfileRequest.class).getResult(UserInfo.getToken())
                .compose(RxHelper.<GetProfileResponse>handleResult())
                .subscribe(new RxSubscribe<GetProfileResponse>(getActivity()) {
                    @Override
                    protected void _onNext(final GetProfileResponse getProfileResponse) {
                        if(getProfileResponse != null && getProfileResponse.getUser() != null){
                            UserInfo.setAccount(getProfileResponse.getUser().getAccount());
                            Common.today_profit = getProfileResponse.getToday_lostwin();
                            if(afterUpdateAccount != null){
                                afterUpdateAccount.afterUpdateAccount(getProfileResponse);
                            }
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return showProgres;
                    }
                });

    }

    @Override
    public void onStart() {
        super.onStart();

        if(getView() != null) {
            getView().setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    removeSoftKeyboard(view);
                    return false;
                }
            });
        }
    }

    protected void removeSoftKeyboard(View view){
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm != null && view != null && view.getWindowToken() != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    //    /**
//     * 通过反射修改TabLayout Indicator的宽度（仅在Android 4.2及以上生效）
//     */
//    protected void setUpIndicatorWidth(TabLayout tabLayout) {
//        Class<?> tabLayoutClass = tabLayout.getClass();
//        Field tabStrip = null;
//        try {
//            tabStrip = tabLayoutClass.getDeclaredField("mTabStrip");
//            tabStrip.setAccessible(true);
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        }
//
//        LinearLayout layout = null;
//        try {
//            if (tabStrip != null) {
//                layout = (LinearLayout) tabStrip.get(tabLayout);
//            }
//            for (int i = 0; i < layout.getChildCount(); i++) {
//                View child = layout.getChildAt(i);
//                child.setPadding(0, 0, 0, 0);
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//                    params.setMarginStart(Utils.dip2px(getActivity(), 30));
//                    params.setMarginEnd(Utils.dip2px(getActivity(), 30));
//                }
//                child.setLayoutParams(params);
//                child.invalidate();
//            }
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
//    }
}
